package com.sunday.common.mq.rocket.brave.study.base.e12_POP;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 生产者数量 与 写队列数量 的关系
 */
@Slf4j
public class PushPopProducer {


    public static void main(String[] args) throws Exception {

//        start();

        batchStart();
    }

    public static void start() throws Exception {

        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("push_pop_producer_group_name");
        // 设置NameServer的地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动Producer实例
        producer.start();

        for (int i = 0; i < 100; i++) {
            // 创建消息，并指定Topic，Tag和消息体
            Message msg = new Message(
                    "PushPopTopic"/* Topic */,
                    "TagA" /* Tag */,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
//            log.info("{}", Thread.currentThread().getName());
            producer.sendOneway(msg);
        }
    }

    /**
     * 默认 topic 会建立 4个队列
     * 生产启动的个数并不会影响到，并且每个队列获取的消息数平均
     * 生产者数量 6 > 写队列数量 4 -> 每个队列的消息数量几乎一致
     * 生产者数量 1 > 写队列数量 4 -> 每个队列的消息数量几乎一致
     */
    public static void batchStart() throws MQClientException {

        for (int i = 0; i < 1; i++) {
            PushPopProducerThread pullProducerThread = new PushPopProducerThread("127.0.0.1:9876", "push_pop_producer_group_name", "PushPopTopic");
            pullProducerThread.start();
        }

    }

}
